package com.zhixianggou.caibaobox.user.service;

import com.zhixianggou.caibaobox.commons.asserts.MyAssert;
import com.zhixianggou.caibaobox.commons.result.ReturnCodeEnum;
import com.zhixianggou.caibaobox.commons.result.ReturnData;
import com.zhixianggou.caibaobox.user.cover.UserCover;
import com.zhixianggou.caibaobox.user.dao.UserMapper;
import com.zhixianggou.caibaobox.user.dto.UserAuthDto;
import com.zhixianggou.caibaobox.user.entities.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author zyq
 * 8/28/24 3:20 PM
 */
@Service
public class UserAuthService {

    @Resource
    UserMapper userMapper;

    public ReturnData register(UserAuthDto dto) {
        User user = userMapper.selectByAccount(dto.getAccount());
        MyAssert.isTrue(user == null, ReturnData.fail(ReturnCodeEnum.ACCOUNT_EXISTS_ERROR).toString());

        user = new User();
        user.setAccount(dto.getAccount());
        user.setLoginPwd(dto.getLoginPwd());
        user.setNickName(dto.getAccount());
        user.setHeadImg("");
        userMapper.insertSelective(user);

        return ReturnData.success();
    }

    /**
     * 获取用户中心信息
     * @param userId
     * @return
     */
    public ReturnData getUserCenterInfo(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        MyAssert.isTrue(user != null && user.getIsDel() == 0, ReturnData.fail(ReturnCodeEnum.ACCOUNT_UN_EXISTS_ERROR).toString());
        MyAssert.isTrue(user.getIsBan() == 0 , ReturnData.fail(ReturnCodeEnum.RC403).toString());
        return ReturnData.success(UserCover.INSTANCE.toCenterDto(user));
    }
}
